#include "Task_Timer.h"

//23 42 41 44 3d 30 2a 34 41 0d 0a
void DistanceTimerHandle(void);
void ButtonTimerHandle(void);
void LoadingContinueTimerHandle(void);
void IRTimerHandle(void);

TimerHandle_t DistanceTimer;
TimerHandle_t Buttton_Timer;
TimerHandle_t LoadingContinue_Timer;

#if BT656
TimerHandle_t IR_Timer;
#endif


QueueHandle_t DistacePower_SemHandle;
QueueHandle_t ButtonComplete_SemHandle;//?? release


void Timer_Create(void)
{
								

    DistanceTimer = xTimerCreate("singalTIMERS",  	//
                            1000*30,       	        //?????(500??),??????
                            pdFALSE,   
                            (void*)1,        
                            (TimerCallbackFunction_t)DistanceTimerHandle); 	//???????
							
	Buttton_Timer = xTimerCreate("singalTIMERS",  	//
                            50,       	        //?????(500??),??????
                            pdFALSE,   
                            (void*)1,        
                            (TimerCallbackFunction_t)ButtonTimerHandle);
#if VL_YT_3578_HD							
	LoadingContinue_Timer = xTimerCreate("singalTIMERS",
                            1*1,  
                            pdFALSE,   
                            (void*)1,        
                            (TimerCallbackFunction_t)LoadingContinueTimerHandle); 
							
#elif VL_MC_105Q
	LoadingContinue_Timer = xTimerCreate("singalTIMERS",
                            8.5*1000,  
                            pdFALSE,   
                            (void*)1,        
                            (TimerCallbackFunction_t)LoadingContinueTimerHandle); 

#endif							
#if BT656 							
	IR_Timer = xTimerCreate("singalTIMERS",
                            10*1000,  
                            pdFALSE,   
                            (void*)1,        
                            (TimerCallbackFunction_t)IRTimerHandle); 														
#endif
							
	ButtonComplete_SemHandle = xSemaphoreCreateBinary(); //??????
}



void DistanceTimerHandle(void)
{
	static uint16_t DistanceTime  = 0;
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	
	//xSemaphoreGive(DistacePower_SemHandle);
	
	LaserPowerOFF();
	OsdRefresh_Family[_LaserFlag].Module_addr[0] = osd_NONE;
	OsdRefresh_Family[_LaserFlag].Module_addr[1] = osd_NONE;
	OsdRefresh_Family[_LaserFlag].Module_addr[2] = '\r';
	OsdRefresh_Family[_LaserFlag].Refresh_Flag = Refresh_yes;
	
	OsdRefresh_Family[_Distance].Module_addr[0] = osd_NONE;
	OsdRefresh_Family[_Distance].Module_addr[1] = osd_NONE;
	OsdRefresh_Family[_Distance].Module_addr[2] = osd_NONE;
	OsdRefresh_Family[_Distance].Module_addr[3] = osd_NONE;
	OsdRefresh_Family[_Distance].Module_addr[4] = osd_NONE;
	OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
	Flag_LsserON = false;

}


void ButtonTimerHandle(void)
{
	Flag_ButtonWaiting = false;//
}


void LoadingContinueTimerHandle(void)
{
	
	bool state;
	gpio_bits_write(GPIOB, GPIO_PINS_2, 1);//

	EraseLoading();
	gpio_bits_write(GPIOB, GPIO_PINS_2, 1);//
//	vTaskDelay(2);

//	//MotorStop_Source = _No_Stop;
//	AmplifierReduce_Date = 0;
//	AmplifierReduce_OsdShow(AmplifierReduce_Date);
//	VL_Zoom_DZoomSet(AmplifierReduce_Date);
//	
//	state = true;
//	xQueueSend(Zoom_QueueHandle,&state,0);

	OsdRefresh_Family[_Daylight_ColorMode].Refresh_Flag = Refresh_yes;
	gpio_bits_write(GPIOB, GPIO_PINS_2, 1);//

	//					OsdRefresh_Family[_VL_Mode].Refresh_Flag = Refresh_yes;
}

#if BT656
void IRTimerHandle(void)
{
    uint8_t num ;
	for(num = 0; num < 100; num++)
	{
	   BT656_Init();
	   vTaskDelay(1000);
	}

}

void StartIRTimer(void)
{
	xTimerStart(IR_Timer, 0);
}

void StopIRTimer(void)
{
	xTimerStop(IR_Timer, 0 );
}

#endif

void StartDistanceTimer(void)
{
	xTimerStart(DistanceTimer, 0);
}

void StopDistanceTimer(void)
{
	xTimerStop(DistanceTimer, 0 );
}

void StartButtonTimer(void)
{
	xTimerStart(Buttton_Timer, 0);
	Flag_ButtonWaiting = true;//??????
}

void StopButtonTimer(void)
{
	xTimerStop(Buttton_Timer, 0);
}

void StartLoadingContinueTimer(void)
{
	xTimerStart(LoadingContinue_Timer, 0);
}

void StopLoadingContinueTimer(void)
{
	xTimerStop(LoadingContinue_Timer, 5);
}